#!/bin/bash
cd ~/app-root/runtime/data/temp/ss/
useragent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0"
sid_grep(){
    i=$1
    sed -i "5c $(grep 'Set-Cookie:' log.txt|cut -d\  -f2|tr '\n' ' '|sed 's#; *$##')" $i
}
cloudflare(){
    i="$1"
    response="$2"
    jschl_vc="$(echo "$response"|grep -o 'jschl_vc\" value=\"[0-9a-zA-Z_\.\-]*'|cut -d\" -f3)"
    pass="$(echo "$response"|grep -o 'pass\" value=\"[0-9a-zA-Z_\.\-]*'|cut -d\" -f3)"
    jschl_answer="$(python ~/app-root/runtime/data/cloudflare.py "$(sed -n '2p' $i)" "$response")"
    sleep 4
    curl "$(sed -n '2p' $i|cut -d '/' -f 3)/cdn-cgi/l/chk_jschl?jschl-vc=$jschl_vc&pass=$pass&jschl_answer=$jschl_answer" --user-agent "$useragent" --Cookie "$(sed -n '5p' $i)" -d "$(sed -n '5p' $i)" -D log.txt 2>&1
}
ss_1(){
    i=$1
    curl -1 "$(sed -n '3p' $i)" --user-agent "$useragent" --Cookie "$(sed -n '5p' $i)" -d "$(sed -n '5p' $i)" -D log.txt > result.txt
    if ! grep 200 log.txt; then
        response="$(curl -1 "$(sed -n '2p' $i)" --user-agent "$useragent" --data "$(sed -n '4p' $i)" --connect-timeout 5 -D log.txt 2>&1)"
        if echo $response|grep -q 'jschl_vc'; then
            sid_grep "$i"
            #cloudflare "$i" "$response"
            #sid_grep "$i"
            curl -1 "$(sed -n '3p' $i)" --user-agent "$useragent" --Cookie "$(sed -n '5p' $i)" -d "$(sed -n '5p' $i)" -D log.txt > result.txt
        else
            sid_grep "$i"
            curl -1 "$(sed -n '3p' $i)" --user-agent "$useragent" --Cookie "$(sed -n '5p' $i)" -d "$(sed -n '5p' $i)" -D log.txt > result.txt
        fi
    fi
}
ss_2(){
    i=$1
    curl -1 "$(sed -n '2p' $i)" --user-agent "$useragent" -H "$(sed -n '3p' $i)" -D log.txt > result.txt
}
ss_3(){
    i=$1
    curl -1 "$(sed -n '3p' $i)" --user-agent "$useragent" --Cookie "$(sed -n '5p' $i)" -D log.txt > result.txt
    if ! grep 200 log.txt; then
        curl -1 "$(sed -n '2p' $i)" --user-agent "$useragent" --data "$(sed -n '4p' $i)" --connect-timeout 5 -D log.txt 2>/dev/null
        sid_grep "$i"
        curl -1 "$(sed -n '3p' $i)" --user-agent "$useragent" --Cookie "$(sed -n '5p' $i)" -D log.txt > result.txt
    fi
}
List=$(ls *.ss)
if [ "$List" ]; then
    for i in $List; do
        word=$(sed -n '2p' $i|awk -F '/' '{print $NF}')
        if echo $word|grep -q '^login$'; then
            ss_1 "$i"
        elif echo $word|grep -q '^_login.php$'; then
            ss_1 "$i"
        elif echo $word|grep -q '^check_in.html$'; then
            ss_2 "$i"
        elif echo $word|grep -q '^login.json$'; then
            ss_3 "$i"
        fi
        if ! grep -q "$(sed -n '1p' $i)：" ~/app-root/repo/log/sign.log; then
            echo "$(sed -n '1p' $i)：从未签到" >> ~/app-root/repo/log/sign.log
        fi
        if grep -q \u result.txt ; then
            return=$(python ~/app-root/runtime/data/github-sh/tool/unicode.py "`cat result.txt|awk -F '\"' '{print $4}'`")
            if echo "$return" | grep -q "流量" ; then
                sed -i "/$(sed -n '1p' $i)：/s#：.*#：$(date -d "8 hour" -u "+%Y年%m月%d日 %H:%M")\t$return#" ~/app-root/repo/log/sign.log
            fi
        elif grep -q "流量" result.txt ; then
            sed -i "/$(sed -n '1p' $i)：/s#：.*#：$(date -d "8 hour" -u "+%Y年%m月%d日 %H:%M")\t$(cat result.txt)#" ~/app-root/repo/log/sign.log
        fi
    done
fi
rm -f *.txt
